package com.atguigu.gmall.pms.service.impl;

import com.atguigu.gmall.common.bean.PageParamVo;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.entity.SkuAttrValueEntity;
import com.atguigu.gmall.pms.entity.SkuEntity;
import com.atguigu.gmall.pms.mapper.AttrMapper;
import com.atguigu.gmall.pms.mapper.SkuAttrValueMapper;
import com.atguigu.gmall.pms.mapper.SkuMapper;
import com.atguigu.gmall.pms.service.SkuAttrValueService;
import com.atguigu.gmall.pms.vo.SaleAttrValueVo;
import com.atguigu.gmall.pms.vo.SkuVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("skuAttrValueService")
public class SkuAttrValueServiceImpl extends ServiceImpl<SkuAttrValueMapper, SkuAttrValueEntity> implements SkuAttrValueService {

    @Autowired
    private AttrMapper attrMapper;

    @Autowired
    private SkuMapper skuMapper;

    @Autowired
    private SkuAttrValueMapper skuAttrValueMapper;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<SkuAttrValueEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<SkuAttrValueEntity>()
        );

        return new PageResultVo(page);
    }

//    @Transactional
    @Override
    public void saveSkuAttrValue(SkuVo skuVo, Long skuId) {
        List<SkuAttrValueEntity> skuAttrValueEntities = skuVo.getSaleAttrs();
        if (CollectionUtils.isEmpty(skuAttrValueEntities)) {
            return;
        }
        skuAttrValueEntities.forEach(skuAttrValueEntity ->
            skuAttrValueEntity.setSkuId(skuId)
        );
        this.saveBatch(skuAttrValueEntities);
    }

    @Override
    public List<SkuAttrValueEntity> querySearchAttrValueBySkuIdAndCid(Long skuId, Long cid) {

        // 1.根据cid查询该分类下检索类型参数（type=1）
        LambdaQueryWrapper<AttrEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AttrEntity::getCategoryId,cid).eq(AttrEntity::getSearchType,1);
        List<AttrEntity> attrEntities = attrMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(attrEntities)){
            return null;
        }

        // 从List<AttrEntity>集合中获取attrId的集合
        List<Long> attrIds = attrEntities.stream().map(AttrEntity::getId).collect(Collectors.toList());

        // 2.查询出当前商品对应的规格参数和值
        LambdaQueryWrapper<SkuAttrValueEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SkuAttrValueEntity::getSkuId,skuId).in(SkuAttrValueEntity::getAttrId,attrIds);
        return this.list(wrapper);
    }

    @Override
    public List<SaleAttrValueVo> querySaleAttrValueBySpuId(Long spuId) {

        // 根据spuId查询所有sku
        List<SkuEntity> skuEntities = this.skuMapper.selectList(new LambdaQueryWrapper<SkuEntity>().eq(SkuEntity::getSpuId, spuId));
        if (CollectionUtils.isEmpty(skuEntities)) {
            return null;
        }

        // 根据skuIds查询所有的销售属性
        List<Long> skuIds = skuEntities.stream().map(SkuEntity::getId).collect(Collectors.toList());
        List<SkuAttrValueEntity> attrValueEntities = this.list(new LambdaQueryWrapper<SkuAttrValueEntity>().in(SkuAttrValueEntity::getSkuId, skuIds).orderByAsc(SkuAttrValueEntity::getAttrId));
        if (CollectionUtils.isEmpty(attrValueEntities)) {
            return null;
        }

        // 转化成需要的数据接口
        Map<Long, List<SkuAttrValueEntity>> map = attrValueEntities.stream().collect(Collectors.groupingBy(SkuAttrValueEntity::getAttrId));
        ArrayList<SaleAttrValueVo> saleAttrValueVos = new ArrayList<>();
        map.forEach((attrId,attrValues) -> {
            SaleAttrValueVo saleAttrValueVo = new SaleAttrValueVo();
            saleAttrValueVo.setAttrId(attrId);
            saleAttrValueVo.setAttrName(attrValues.get(0).getAttrName());
            saleAttrValueVo.setAttrValues(attrValues.stream().map(SkuAttrValueEntity::getAttrValue).collect(Collectors.toSet()));
            saleAttrValueVos.add(saleAttrValueVo);
        });
        return saleAttrValueVos;
    }

    @Override
    public Map<String, Long> queryMappingBySpuId(Long spuId) {
        // 根据spuId查询所有sku
        List<SkuEntity> skuEntities = this.skuMapper.selectList(new LambdaQueryWrapper<SkuEntity>().eq(SkuEntity::getSpuId, spuId));
        if (CollectionUtils.isEmpty(skuEntities)) {
            return null;
        }

        // 根据skuIds查询所有的销售属性
        // 获取skuId集合
        List<Long> skuIds = skuEntities.stream().map(SkuEntity::getId).collect(Collectors.toList());

        // map:{sku_id=19, attrValues=金色,8G,512G}
        List<Map<String, Object>> maps = skuAttrValueMapper.queryMappingBySpuId(skuIds);
        if (CollectionUtils.isEmpty(maps)) {
            return null;
        }
        
        return maps.stream().collect(Collectors.toMap(map -> map.get("attrValues").toString(), map -> (Long)map.get("sku_id")));
    }
}